package com.sqstudio.control
{
	import com.sqstudio.common.Sys;
	import com.sqstudio.common.TipManager;
	import com.sqstudio.event.CalendarEvent;
	import com.sqstudio.model.Model;
	import com.sqstudio.ui.HistoryMC;
	import com.sqstudio.ui.HistoryTip;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.media.Camera;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	
	/**
	 *历史今天控制器 
	 * @author Nestor
	 * 
	 */	
	public class HistoryController
	{
		private var historyBtn:HistoryMC;
		private var historyTip:HistoryTip;
		public function HistoryController()
		{
			this.historyBtn = new HistoryMC();
			this.historyBtn.buttonMode = true;
			var dobj:DisplayObject = Sys.STAGE.addChild(this.historyBtn);
			dobj.x = 405;
			dobj.y = 407;
			
			this.historyTip = new HistoryTip();
			Sys.STAGE.addChild(this.historyTip);
			this.historyTip.visible = false;
			
			var hTxtArr:Array = [this.historyTip.h1_txt,this.historyTip.h2_txt,this.historyTip.h3_txt,this.historyTip.h4_txt,this.historyTip.h5_txt];
			
			for(var i:int=0;i<hTxtArr.length;i++){
				TextField(hTxtArr[i]).mouseEnabled = TextField(hTxtArr[i]).selectable = false;
				TextField(hTxtArr[i]).text = "正在加载信息...";
			}
			
			new TipManager(this.historyBtn,this.historyTip,-10);
			this.historyTip.alpha = 0;
			this.historyBtn.addEventListener(MouseEvent.CLICK,hisCKHandler);
			
			CalendarEvent.Dispatcher.addEventListener(CalendarEvent.UPDATE_HISTORY,historyParse);
		}
		
		
		/**
		 *解析历史今天的数据 
		 * 
		 */		
		private function historyParse(e:CalendarEvent):void{
			var obj:Object = Model.getInstance().history_obj;
			
			var hisArr:Array = new Array();
			for each(var o:Object in obj ){
				hisArr.push(o.name+" "+o.value);
			}
			if(hisArr.length ==0 ) return;
			var hTxtArr:Array = [this.historyTip.h1_txt,this.historyTip.h2_txt,this.historyTip.h3_txt,this.historyTip.h4_txt,this.historyTip.h5_txt];

			for(var i:int=0;i<hTxtArr.length;i++){
				TextField(hTxtArr[i]).text = hisArr[i];
			}
			this.historyTip.alpha = 1;
		}
		
		/**
		 *跳转到百度百科历史今天 
		 * @param e
		 * 
		 */		
		private function hisCKHandler(e:MouseEvent):void
		{
			var date:Date = new Date();
			var day:String = (date.getMonth()+1)+"%D4%C2"+date.getDate()+"%C8%D5";
			var link:String = "http://baike.baidu.com/list-php/dispose/searchword.php?word="+day+"&pic=1";
			navigateToURL(new URLRequest(link),"_blank");
		}
	}
}